﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Contract;
using System.Data.SqlClient;
using System.Data;
using System.ServiceModel;
namespace Service
{
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, UseSynchronizationContext = false, IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.Single)]
    class DataUploader : IDataUploader
    {

        SqlConnection con = new SqlConnection(service.Properties.Settings.Default.Database1ConnectionString1);

        public void Upload(MyFile Mf)
        {
            string ServerPath = Path.Combine("Storage", Mf.Name);
            using (FileStream fs = new FileStream(ServerPath, FileMode.Create))
        {
            int bufferSize = 1 * 1024 * 1024; // 1 MB buffer
            byte[] buffer = new byte[bufferSize];
            int bytes;

            while ((bytes = Mf.Value.Read(buffer, 0, bufferSize)) > 0)
            {
                fs.Write(buffer, 0, bytes);
                fs.Flush();
            }
            string query = "INSERT INTO Table1 (FILENAME) SELECT ('" + Mf.Name.ToString() + "') WHERE not exists(select * from Table1 where FILENAME =('" + Mf.Name.ToString() + "'))";
            SqlCommand insert = new SqlCommand(query, con);
            con.Open();
            insert.ExecuteNonQuery();
            con.Close();
        }
            
    }
     
        public Stream Download(string name)
        {
            try
            {
                string fPath = Path.Combine("Storage", name);
                return new FileStream(fPath, FileMode.Open, FileAccess.Read);
            }catch(FileNotFoundException)
            {
                throw new FileNotFoundException("File not found",name);
            }
           
        }

        public void Delete(string name)
        {
            string serverPath = Path.Combine("Storage", name);

            if (File.Exists(serverPath))
            {
                File.Delete(serverPath);
            }
            string query = "DELETE FROM Table1 WHERE FILENAME='" +name + "'";
            SqlCommand delete = new SqlCommand(query, con);
            con.Open();
            delete.ExecuteNonQuery();
            con.Close();
            
        }

        public MyFile[] refreshList()
        {
            string query = "SELECT DISTINCT FILENAME FROM Table1";
            string checkCount = "SELECT COUNT(DISTINCT FILENAME) FROM Table1";
            SqlCommand getall = new SqlCommand(query, con);
            SqlCommand count = new SqlCommand(checkCount, con);
            
            
            con.Open();
            var num = count.ExecuteScalar();
            con.Close();
            
            con.Open();
            var read= getall.ExecuteReader();
        
            string names = "";

            int i = 0;
            MyFile[] mfs = new MyFile[Convert.ToInt32(num)];
            while (read.Read())
            {
                names = read["FILENAME"].ToString();
            
             
                MyFile mf = new MyFile() { Name = read["FILENAME"].ToString(), Size = 0, Value = null };
                mfs[i] = mf;
                i++;
            }
            con.Close();

            return mfs;}

 

    }

    }

